/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package eu.fsiebert.polytech.db.mediatheque.entity;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

/**
 *
 * @author Florent
 */
@Entity
@Table(name = "Item")
@NamedQueries({
    @NamedQuery(name = "Item.findAll", query = "SELECT i FROM Item i"),
    @NamedQuery(name = "Item.findByItemRef", query = "SELECT i FROM Item i WHERE i.itemRef = :itemRef"),
    @NamedQuery(name = "Item.findByRef", query = "SELECT i FROM Item i WHERE i.ref = :ref"),
    @NamedQuery(name = "Item.findByDateAjout", query = "SELECT i FROM Item i WHERE i.dateAjout = :dateAjout"),
    @NamedQuery(name = "Item.findByEtat", query = "SELECT i FROM Item i WHERE i.etat = :etat"),
    @NamedQuery(name = "Item.findByDateParution", query = "SELECT i FROM Item i WHERE i.dateParution = :dateParution"),
    @NamedQuery(name = "Item.findNewItems", query = "SELECT i FROM Item i ORDER BY i.dateAjout DESC")
})
public class Item implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ItemRef", nullable = false)
    private Integer itemRef;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 13)
    @Column(name = "Ref", nullable = false, length = 13)
    private String ref;
    @Basic(optional = false)
    @NotNull
    @Column(name = "DateAjout", nullable = false)
    @Temporal(TemporalType.TIMESTAMP)
    private Date dateAjout;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 10)
    @Column(name = "Etat", nullable = false, length = 10)
    private String etat;
    @Column(name = "DateParution")
    @Temporal(TemporalType.DATE)
    private Date dateParution;
    @ManyToMany(mappedBy = "itemList")
    private List<CommandeDuJour> commandeDuJourList;
    @JoinTable(name = "ItemTag", joinColumns = {
        @JoinColumn(name = "ItemRef", referencedColumnName = "ItemRef", nullable = false)}, inverseJoinColumns = {
        @JoinColumn(name = "Tag", referencedColumnName = "Tag", nullable = false)})
    @ManyToMany
    private List<Tag> tagList;
    @OneToMany(mappedBy = "itemRef")
    private List<Exemplaire> exemplaireList;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "itemRef")
    private List<Reservation> reservationList;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "item")
    private List<LigneDeCommande> ligneDeCommandeList;
    @JoinColumn(name = "Type", referencedColumnName = "Id", nullable = false)
    @ManyToOne(optional = false)
    private TypeItem type;
    @OneToOne(cascade = CascadeType.ALL, mappedBy = "item")
    private Magazine magazine;
    @OneToOne(cascade = CascadeType.ALL, mappedBy = "item")
    private Dvd dvd;
    @OneToOne(cascade = CascadeType.ALL, mappedBy = "item")
    private Cd cd;
    @OneToOne(cascade = CascadeType.ALL, mappedBy = "item")
    private Revue revue;
    @OneToOne(cascade = CascadeType.ALL, mappedBy = "item")
    private Vhs vhs;
    @OneToOne(cascade = CascadeType.ALL, mappedBy = "item")
    private Livre livre;

    @Transient
    private Class typeItem;
    
    @Transient
    private Exemplaire exemplaireSelected;
    
    public Exemplaire getExemplaireSelected() {
        return exemplaireSelected;
    }
    
     public void setExemplaireSelected(Exemplaire ex) {
        exemplaireSelected = ex;
    }
    
    public Item() {
    }

    public Item(Integer itemRef) {
        this.itemRef = itemRef;
    }

    public Item(Integer itemRef, String ref, Date dateAjout, String etat) {
        this.itemRef = itemRef;
        this.ref = ref;
        this.dateAjout = dateAjout;
        this.etat = etat;
    }

    public Integer getItemRef() {
        return itemRef;
    }

    public void setItemRef(Integer itemRef) {
        this.itemRef = itemRef;
    }

    public String getRef() {
        return ref;
    }

    public void setRef(String ref) {
        this.ref = ref;
    }

    public Date getDateAjout() {
        return dateAjout;
    }

    public void setDateAjout(Date dateAjout) {
        this.dateAjout = dateAjout;
    }

    public String getEtat() {
        return etat;
    }

    public void setEtat(String etat) {
        this.etat = etat;
    }

    public Date getDateParution() {
        return dateParution;
    }
    
    public boolean getFreeState() {
        
        return true;
    }

    public void setDateParution(Date dateParution) {
        this.dateParution = dateParution;
    }

    public List<CommandeDuJour> getCommandeDuJourList() {
        return commandeDuJourList;
    }

    public void setCommandeDuJourList(List<CommandeDuJour> commandeDuJourList) {
        this.commandeDuJourList = commandeDuJourList;
    }

    public List<Tag> getTagList() {
        return tagList;
    }

    public void setTagList(List<Tag> tagList) {
        this.tagList = tagList;
    }

    public List<Exemplaire> getExemplaireList() {
        return exemplaireList;
    }

    public void setExemplaireList(List<Exemplaire> exemplaireList) {
        this.exemplaireList = exemplaireList;
    }

    public List<Reservation> getReservationList() {
        return reservationList;
    }

    public void setReservationList(List<Reservation> reservationList) {
        this.reservationList = reservationList;
    }

    public List<LigneDeCommande> getLigneDeCommandeList() {
        return ligneDeCommandeList;
    }

    public void setLigneDeCommandeList(List<LigneDeCommande> ligneDeCommandeList) {
        this.ligneDeCommandeList = ligneDeCommandeList;
    }

    public TypeItem getType() {
        return type;
    }

    public void setType(TypeItem type) {
        this.type = type;
    }

    public Class getTypeItem() {
        return typeItem;
    }

    public Magazine getMagazine() {
        return magazine;
    }

    public void setMagazine(Magazine magazine) {
        this.magazine = magazine;
        if(magazine != null) this.typeItem = Magazine.class;
    }

    public Dvd getDvd() {
        return dvd;
    }

    public void setDvd(Dvd dvd) {
        this.dvd = dvd;
        if(dvd != null) this.typeItem = Dvd.class;
    }
    public Cd getCd() {
        return cd;
    }

    public void setCd(Cd cd) {
        this.cd = cd;
        if(cd != null) this.typeItem = Cd.class;
    }

    public Revue getRevue() {
        return revue;
    }

    public void setRevue(Revue revue) {
        this.revue = revue;
        if(revue != null) this.typeItem = Revue.class;
    }

    public Vhs getVhs() {
        return vhs;
    }

    public void setVhs(Vhs vhs) {
        this.vhs = vhs;
        if(vhs != null) this.typeItem = Vhs.class;
    }

    public Livre getLivre() {
        return livre;
    }

    public void setLivre(Livre livre) {
        this.livre = livre;
        if(livre != null) this.typeItem = Livre.class;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (itemRef != null ? itemRef.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Item)) {
            return false;
        }
        Item other = (Item) object;
        if ((this.itemRef == null && other.itemRef != null) || (this.itemRef != null && !this.itemRef.equals(other.itemRef))) {
            return false;
        }
        return true;
    }

    public String getLibelle() {
                if(magazine != null) {
            return "Magazine ref. : "+ref+" ("+magazine.getTitre()+")";
        }
        if(dvd != null) {
            return "DVD ref. : "+ref+" ("+dvd.getTitre()+")";
        }
        if(cd != null) {
            return "CD ref. : "+ref+" (de "+cd.getArtiste()+")";
        }
        if(revue != null) {
            return "Revue ref. : "+ref+" ("+revue.getTitre()+")";
        }
        if(vhs != null) {
            return "VHS ref. : "+ref+" ("+vhs.getTitre()+")";
        }
        if(livre != null) {
            return "Livre ref. : "+ref+" ("+livre.getTitre()+" de "+livre.getAuteur()+")";
        }
        return "Item ref. : "+ref;
    }
    
    @Override
    public String toString() {
        return getLibelle();
    }
    
}
